package com.epower.web.action.epowerbook;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerbook.EpowerBookService;
import com.epower.dataobject.EpowerBookDO;

@Scope("prototype")
@Component()
public class EpowerBookAction extends BaseAction {
    
	private static final long serialVersionUID = 1L;
	
	/***查询对象 ***/
	private EpowerBookDO query;
	
	/***对象集合 ***/
	private List<EpowerBookDO> epowerBookList;
	
	/*** BO ***/
	private EpowerBookService epowerBookBO;
	
	private String bookName;
	/***主键***/
	private Integer id;
	
	/***查询对象 ***/
	private EpowerBookDO epowerBook;
	
	/***当前action所在的namespace ***/
	private String namespace="epowerbook";
	/***当前action对应的module ***/
	private String moduleName="EpowerBook";
	
	private Result result = Result.succResult();
	/***
	 * 列表视图
	 * @return
	 * @throws Exception
	 */
	public String listEpowerBook() throws Exception {
		//初始化查询条件
		if (query == null) {
			query = new EpowerBookDO();
		}
		//查询列表
		epowerBookList = epowerBookBO.getEpowerBookListPageByOption(query);
		pages = this.getPagesList(query.getToPage(), query.getTotalPage());
		return SUCCESS;
	}
	
	public String listEpowerBookByName(){
	    if(bookName == null){
	        return ERROR;
	    }
	    EpowerBookDO bookDO = new EpowerBookDO();
	    bookDO.setLibname(bookName);
	    epowerBookList = epowerBookBO.getEpowerBookListByName(bookDO);
	    List<TmpBook> tmps = new ArrayList<TmpBook>();
	    for(EpowerBookDO tmp:epowerBookList){
	        TmpBook tmpBook = new TmpBook();
	        tmpBook.setId(tmp.getId());
	        tmpBook.setPid(tmp.getPid());
	        tmpBook.setLibname(tmp.getLibname());
	        tmps.add(tmpBook);
	    }
	    Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("books", tmps);
        result.setResult(resultMap);
	    return SUCCESS;
	}
	/***
	 * 编辑视图
	 * @return
	 * @throws Exception
	 */
	public String viewEditEpowerBook() throws Exception{
		epowerBook=epowerBookBO.getEpowerBookById(id);
		return SUCCESS;
	}
	
	/***
	 * 编辑操作
	 * @return
	 * @throws Exception
	 */
	 
	public String editEpowerBook()throws Exception{
		if(!epowerBookBO.updateEpowerBookByOption(epowerBook)){
		    addActionMessage("编辑失败");
			return SUCCESS;
		}
		addActionMessage("编辑成功");
		return SUCCESS;
	}
	
	/***
	 * 删除操作
	 * @return
	 * @throws Exception
	 */
	public String deleteEpowerBook()throws Exception{
		if(!epowerBookBO.deleteEpowerBookById(id)){
			addActionMessage("删除失败");
			return SUCCESS;
		}
		addActionMessage("删除成功");
		return SUCCESS;
	}
	/***
	 * 添加视图
	 * @return
	 * @throws Exception
	 */
	public String viewAddEpowerBook()throws Exception{
		return SUCCESS;
	}
	
	/***
	 * 添加操作
	 * @return
	 * @throws Exception
	 */
	public String addEpowerBook()throws Exception{
		if(epowerBookBO.addEpowerBook(epowerBook)==null){
			addActionMessage("添加失败");
			return SUCCESS;
		}
		addActionMessage("添加成功");
		return SUCCESS; 
	}
	
	
	
	/*** setter getter start***/
	public EpowerBookDO getQuery() {
		return query;
	}
	public void setQuery(EpowerBookDO query) {
		this.query = query;
	}
	public List<EpowerBookDO> getEpowerBookList() {
		return epowerBookList;
	}
	public void setEpowerBookList(List<EpowerBookDO> epowerBookList) {
		this.epowerBookList = epowerBookList;
	}
	public EpowerBookService getEpowerBookBO() { 
		return epowerBookBO;
	}
	@Resource
	public void setEpowerBookBO(EpowerBookService epowerBookBO) {
		this.epowerBookBO = epowerBookBO;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public EpowerBookDO getEpowerBook() {
		return epowerBook;
	}
	public void setEpowerBook(EpowerBookDO epowerBook) {
		this.epowerBook = epowerBook;
	}
	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}
	
	public String getModuleName() {
		return moduleName;
	}
	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
    
    public String getBookName() {
        return bookName;
    }
    
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    
    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
	/*** setter getter end***/
    
    public class TmpBook{
        private Integer id;
        private Integer pid;
        private String libname;

        public Integer getId() {
            return id;
        }
        
        public void setId(Integer id) {
            this.id = id;
        }

        public String getLibname() {
            return libname;
        }

        public void setLibname(String libname) {
            this.libname = libname;
        }
        
        public Integer getPid() {
            return pid;
        }

        public void setPid(Integer pid) {
            this.pid = pid;
        }
        
    }
}

